<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch"
             label-width="98px">
      <el-form-item label="商品名称" prop="name">
        <el-input v-model="queryParams.name" placeholder="请输入商品名称"
                  clearable @keyup.enter.native="handleQuery"/>
      </el-form-item>
      <el-form-item label="商品品牌" prop="brand">
        <el-input v-model="queryParams.brand" placeholder="请输入商品品牌"
                  clearable @keyup.enter.native="handleQuery"/>
      </el-form-item>
      <el-form-item label="商品型号" prop="model">
        <el-input v-model="queryParams.model" placeholder="请输入商品型号"
                  clearable @keyup.enter.native="handleQuery"/>
      </el-form-item>
      <el-form-item label="采购人姓名" prop="buyName">
        <el-input v-model="queryParams.buyName" placeholder="请输入采购人姓名"
                  clearable @keyup.enter.native="handleQuery"/>
      </el-form-item>
      <el-form-item label="采购方式" prop="buyType">
        <el-select v-model="queryParams.buyType" placeholder="采购方式" clearable style="width: 130px">
          <el-option v-for="dict in dict.type.buy_type"
                     :key="dict.value" :label="dict.label" :value="dict.value"/>
        </el-select>
      </el-form-item>
      <el-form-item label="是否报销" prop="reimburse">
        <el-select v-model="queryParams.reimburse" placeholder="是否报销" clearable style="width: 130px">
          <el-option v-for="dict in dict.type.yes_no"
                     :key="dict.value" :label="dict.label" :value="dict.value"/>
        </el-select>
      </el-form-item>
      <!--<el-form-item label="是否取得发票" prop="invoice">
        <el-select v-model="queryParams.invoice" placeholder="是否取得发票" clearable style="width: 130px">
          <el-option v-for="dict in dict.type.yes_no"
                     :key="dict.value" :label="dict.label" :value="dict.value"/>
        </el-select>
      </el-form-item>-->
      <el-form-item label="供应商名称" prop="supplierName">
        <el-input v-model="queryParams.supplierName" placeholder="请输入供应商名称"
                  clearable @keyup.enter.native="handleQuery"/>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <!--<el-row :gutter="10" class="mb8">
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>-->

    <el-col :span="1.5">
      <el-button type="primary" plain icon="el-icon-edit" size="mini" @click="handleBatch({}, '1')">零散销售</el-button>
    </el-col>
    <el-col :span="1.5" style="margin-left: 20px;">
      <el-button type="primary" plain icon="el-icon-edit" size="mini" @click="handleBatch({}, '2')">项目销售</el-button>
    </el-col>
    <!--<el-col :span="1.5" style="margin-left: 20px;">
      <el-button type="primary" plain icon="el-icon-edit" size="mini" @click="handleBatch({}, '3')">项目费用</el-button>
    </el-col>-->
    <el-col :span="1.5" style="margin-left: 20px;">
      <el-button type="warning" plain icon="el-icon-download" size="mini" @click="handleExport">导出
      </el-button>
    </el-col>

    <el-table border v-loading="loading" :data="dataList" @selection-change="handleSelectionChange"
              :max-height="tableMaxHeight">
      <el-table-column type="selection" width="55" align="center"/>
      <!--                    <el-table-column label="${comment}" align="center" prop="id"/>-->
      <el-table-column label="商品名称" align="center" prop="name"/>
      <el-table-column label="商品品牌" align="center" prop="brand"/>
      <el-table-column label="商品型号" align="center" prop="model"/>
      <el-table-column label="商品单位" align="center" prop="unit"/>
      <el-table-column label="含税单价" align="center" prop="costPriceTax"/>
      <!-- <el-table-column label="采购数量" align="center" prop="count"/>-->
      <el-table-column label="库存数量" align="center" prop="remainderCount"/>
      <!--<el-table-column label="采购总价" align="center" prop="cost"/>
      <el-table-column label="有效日期" align="center" prop="validityTerm" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.validityTerm, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>-->
      <!--      <el-table-column label="采购人" align="center" prop="buyId"/>-->
      <el-table-column label="采购人姓名" align="center" prop="buyName"/>
      <el-table-column label="采购日期" align="center" prop="buyDate" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.buyDate, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <!--<el-table-column label="采购方式" align="center" prop="buyType">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.buy_type" :value="scope.row.buyType"/>
        </template>
      </el-table-column>
      <el-table-column label="是否报销" align="center" prop="reimburse">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.yes_no" :value="scope.row.reimburse"/>
        </template>
      </el-table-column>-->
      <!--      <el-table-column label="供应商" align="center" prop="supplierId"/>-->
      <!--<el-table-column label="供应商名称" align="center" prop="supplierName"/>
      <el-table-column label="是否取得发票" align="center" prop="invoice">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.yes_no" :value="scope.row.invoice"/>
        </template>
      </el-table-column>
      <el-table-column label="发票编号" align="center" prop="invoiceNo"/>-->
      <!--            <el-table-column label="备注" align="center" prop="remark"/>-->
      <!--<el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button size="mini" type="text" icon="el-icon-edit" v-if="scope.row.remainderCount > 0"
                     @click="handleBatch(scope.row)">销售
          </el-button>
        </template>
      </el-table-column>-->
    </el-table>

    <pagination v-show="total>0" :total="total" :page.sync="queryParams.pageNum"
                :limit.sync="queryParams.pageSize" @pagination="getList"/>
    <!--    <goods-sale ref="goodsSale" @ok="getList"></goods-sale>-->
    <goods-sale-batch ref="goodsSaleBatch" @ok="getList"></goods-sale-batch>
    <goods-sale-batch-for-prj ref="goodsSaleBatchForPrj" @ok="getList"></goods-sale-batch-for-prj>
  </div>
</template>

<script>
    import {deleteAction, downloadFile, getAction} from '@/api/manage'
    // import GoodsSale from "./module/GoodsSale";
    import GoodsSaleBatch from "./module/GoodsSaleBatch";
    import GoodsSaleBatchForPrj from "./module/GoodsSaleBatchForPrj";

    export default {
        name: "GoodsOrderInList",
        dicts: ['yes_no', 'buy_type', 'goods_supplier'],
        components: {
            // GoodsSale,
            GoodsSaleBatch,
            GoodsSaleBatchForPrj,
        },
        data() {
            return {
                // 遮罩层
                loading: true,
                // 选中数组
                ids: [],
                // 非单个禁用
                single: true,
                // 非多个禁用
                multiple: true,
                // 显示搜索条件
                showSearch: true,
                // 总条数
                total: 0,
                // 入库信息管理表格数据
                dataList: [],
                // 查询参数
                queryParams: {
                    pageNum: 1,
                    pageSize: 20,
                    name: null,
                    brand: null,
                    unit: null,
                    costPrice: null,
                    count: null,
                    cost: null,
                    validityTerm: null,
                    buyId: null,
                    buyName: null,
                    buyDate: null,
                    buyType: null,
                    reimburse: null,
                    supplierId: null,
                    supplierName: null,
                    params: {
                        dataFlag: "1"
                    },
                    orderByColumn: "buy_date",
                    isAsc: "desc"
                },
                url: {
                    list: '/goodsOrderIn/goodsOrderIn/list',
                    remove: '/goodsOrderIn/goodsOrderIn/',
                    export: '/goodsOrderIn/goodsOrderIn/export',
                },
                tableMaxHeight: window.innerHeight - 320,
            };
        },
        created() {
            this.getList();
        },
        methods: {
            handleSale(row) {
                this.$refs.goodsSale.sale(row);
            },
            /** 查询入库信息管理列表 */
            getList() {
                this.loading = true;
                getAction(this.url.list, this.queryParams).then(response => {
                    this.dataList = response.rows;
                    this.total = response.total;
                    this.loading = false;
                });
            },
            /** 搜索按钮操作 */
            handleQuery() {
                this.queryParams.pageNum = 1;
                this.getList();
            },
            /** 重置按钮操作 */
            resetQuery() {
                this.resetForm("queryForm");
                this.handleQuery();
            },
            // 多选框选中数据
            handleSelectionChange(selection) {
                this.ids = selection.map(item => item.id)
                this.single = selection.length !== 1
                this.multiple = !selection.length
            },
            /** 新增按钮操作 */
            handleAdd() {
                this.$refs.form.handleAdd();
            },

            /** 新增按钮操作 */
            handleBatch(row, type) {
                const ids = row.id || this.ids;
                if (ids == null || ids == '' || ids == 'undefined') {
                    alert("请选择一条记录");
                } else {
                    if (type == '1') {
                        this.$refs.goodsSaleBatch.show(ids);
                    } else {
                        this.$refs.goodsSaleBatchForPrj.show(ids, type);
                    }
                }
            },
            /** 删除按钮操作 */
            handleDelete(row) {
                const ids = row.id || this.ids;
                deleteAction(this.url.remove, ids).then(response => {
                    this.getList();
                    this.$modal.msgSuccess("删除成功");
                }).catch(() => {
                });
            },
            /** 导出按钮操作 */
            handleExport() {
                let that = this;
                that.loading = false;
                that.$confirm('是否确认导出商品库存信息表?', "确认", {
                    confirmButtonText: "确定",
                    cancelButtonText: "取消",
                    type: "warning"
                }).then(function () {
                    downloadFile('/goodsOrderIn/goodsOrderIn/exportKc', that.queryParams);
                });
            },
        }
    };
</script>
